//
//  loginPassWordViewController.m
//  HJB_project
//
//  Created by jiezsoft.com on 14-11-12.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.
//

#import "UpdateThePasswordViewController.h"
#import "TheLoginViewController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"

@interface UpdateThePasswordViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate ,UINavigationControllerDelegate>

@end

@implementation UpdateThePasswordViewController

{
    UITextField* userNameTextField;
    UITextField* passwordTextField;
    UITextField* passwordTextField1;
    UITextField* new;
    UIView* myToolBar;
    UIButton*agreementBtn;
    UIButton*agreementBtn1;
    BOOL saveAccount;
    BOOL saveAccount1;
    UIButton*login;
}
@synthesize alt = alt;
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    self.view.backgroundColor=[UIColor whiteColor];
    // 手势
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapDone)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self.view addGestureRecognizer:singleFingerOne];
    
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    
    self.view.backgroundColor=[UIColor whiteColor];
    UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(20,170*ScreenHeight/1334+64, ScreenWidth-40, 0.40)];
    line1.backgroundColor = qianhuiColor;
    [self.view addSubview:line1];
    UILabel *line3 = [[UILabel alloc]initWithFrame:CGRectMake(20,255*ScreenHeight/1334+64, ScreenWidth-40, 0.40)];
    line3.backgroundColor = qianhuiColor;
    [self.view addSubview:line3];
    UILabel *line13 = [[UILabel alloc]initWithFrame:CGRectMake(20,345*ScreenHeight/1334+64, ScreenWidth-40, 0.40)];
    line13.backgroundColor = qianhuiColor;
    [self.view addSubview:line13];

    userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f,90*ScreenHeight/1334+64, ScreenWidth-40,80*ScreenHeight/1334)];
    [userNameTextField setBorderStyle:UITextBorderStyleNone]; //外框类型
    userNameTextField.secureTextEntry = YES; //密码
    userNameTextField.placeholder = @"请输入当前旧密码"; //默认显示的字
    userNameTextField.font=[UIFont systemFontOfSize:contenfot];
    userNameTextField.returnKeyType = UIReturnKeyDone;
    userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    userNameTextField.delegate = self;
    userNameTextField.tag=22;
    [self.view addSubview:userNameTextField];
    
    
    myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:keyboardFot];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];
    userNameTextField.inputAccessoryView = myToolBar;
    
    
    passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,170*ScreenHeight/1334+64, ScreenWidth-40,80*ScreenHeight/1334)];
    [passwordTextField setBorderStyle:UITextBorderStyleNone]; //外框类型
    passwordTextField.placeholder = @"请输入新密码"; //默认显示的字
    passwordTextField.secureTextEntry = YES; //密码
    passwordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    passwordTextField.returnKeyType = UIReturnKeyDone;
    passwordTextField.font=[UIFont systemFontOfSize:contenfot];
    passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    passwordTextField.delegate = self;
    [self.view addSubview:passwordTextField];
    passwordTextField.inputAccessoryView = myToolBar;
    

    passwordTextField1 = [[UITextField alloc] initWithFrame:CGRectMake(20,260*ScreenHeight/1334+64, ScreenWidth-40,80*ScreenHeight/1334)];
    [passwordTextField1 setBorderStyle:UITextBorderStyleNone]; //外框类型
    passwordTextField1.placeholder = @"请再次输入新密码"; //默认显示的字
    passwordTextField1.secureTextEntry = YES; //密码
    passwordTextField1.autocorrectionType = UITextAutocorrectionTypeNo;
    passwordTextField1.autocapitalizationType = UITextAutocapitalizationTypeNone;
    passwordTextField1.returnKeyType = UIReturnKeyDone;
    passwordTextField1.font=[UIFont systemFontOfSize:contenfot];
    passwordTextField1.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    passwordTextField1.delegate = self;
    [passwordTextField1 addTarget:self action:@selector(passwordTextFieldLength:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:passwordTextField1];
    passwordTextField1.inputAccessoryView = myToolBar;

    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(80, passwordTextField1.bottom, ScreenWidth-100, 44)];
    label.text=@"密码由6-20位英文字母或数字组成";
    label.textAlignment=NSTextAlignmentRight;
    [label setTextColor:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0]];
    label.font = [UIFont systemFontOfSize:40/3];
    [self.view addSubview:label];

    
   [self loginBtn];
    
    


    
    
    
    // Do any additional setup after loading the view from its nib.
}


-(void)loginBtn{
    [login removeFromSuperview];
    login = [[UIButton alloc] initWithFrame:CGRectMake(10, 460*ScreenHeight/1334+64, ScreenWidth-20, anniuHeigth)];
    [login setTitle:@"确认修改" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if (passwordTextField1.text.length == 0) {
        
        [login.layer setCornerRadius:3.0]; //设置矩圆角半径
        [login.layer setBorderWidth:0.3];   //边框宽度
        CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){228/255.0, 228/255.0, 228/255.0, 1.0});
        [login.layer setBorderColor:tureBTncolorref];//边框颜色
        login.backgroundColor=qianhuiColor;
        [self.view addSubview:login];
    }if (passwordTextField1.text.length >1) {
        
        login.backgroundColor=luseColor;
        [login.layer setCornerRadius:3.0]; //设置矩圆角半径
        [login.layer setBorderWidth:0.3];   //边框宽度
        CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){23/255.0, 148/255.0, 152/255.0, 1.0});
        [login.layer setBorderColor:tureBTncolorref];//边框颜色
        [login addTarget:self action:@selector(ture:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:login];
    }
}


-(void)ture:(UIButton*)sender{
    
    if ([passwordTextField.text length] < 6) {
        [self showAlertMessage:@"登录密码不可以少于6位"];
    } else{
        if (![passwordTextField.text isEqualToString:passwordTextField1.text])
        {
            [self showAlertMessage:@"新密码两次不一致"];
        }
        else
        {

        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:Token forKey:@"tokenId"];
        [params setValue:[MyMD5 md5:userNameTextField.text] forKey:@"loginWord"];
        [params setValue:[MyMD5 md5:passwordTextField.text] forKey:@"newPassword"];
        [params setValue:[MyMD5 md5:passwordTextField1.text] forKey:@"rNewPassword"];
          NSLog(@"33333333====%@",params);
            //Common
      [HttpRequest requestData1:@"Common/UpdatePassword" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
        
                    NSLog(@"33333333====%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                
                
                NSString*Password=passwordTextField.text;
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                
                [user setObject:Password forKey:@"passwordTextField"];
                
                [user synchronize];
                [userNameTextField resignFirstResponder];
                [passwordTextField resignFirstResponder];
                [passwordTextField1 resignFirstResponder];
                TheLoginViewController* Login=[[TheLoginViewController alloc]init];
                [self.navigationController pushViewController:Login animated:YES];

            
        }else{
  
            NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
            
            [Global showTextHud:requestResultStr view:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    TheLoginViewController* Login=[[TheLoginViewController alloc]init];
                    
                    [self.navigationController pushViewController:Login animated:YES];
                    
                                        }
                                    }
                                }
                                else
                                {
                                    [Global hideProgressHUDInternal:self.view];
                                    
                                    [Global showTextHudSub:severDataDict.resultStr view:self.view];
                                }
                                
                            }];
        
        }
    }
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}



- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)passwordTextFieldLength:(UITextField*)textField
{
    if ([textField.text length] > 2)
    {
        [self loginBtn];
    }
}


-(void)OnTapDone{
    [userNameTextField resignFirstResponder];
    [passwordTextField resignFirstResponder];
    [passwordTextField1 resignFirstResponder];
    
    
    
}


@end
